package org.kust.bbs.user.controller;


import org.kust.bbs.common.utils.JwtUtils;
import org.kust.bbs.common.web.JsonResult;
import org.kust.bbs.pojo.entity.User;
import org.kust.bbs.pojo.vo.RegisterVO;
import org.kust.bbs.pojo.vo.RetrievePasswordVO;
import org.kust.bbs.pojo.vo.UpdatePasswordVO;
import org.kust.bbs.pojo.vo.UserCodeLoginVO;
import org.kust.bbs.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 用户密码登录(登录成功时存入redis,从redis中获取token)
     *
     * @param user
     * @param request
     * @return
     */
    @GetMapping("/login")
    public JsonResult<String> login(User user, HttpServletRequest request) {
        //返回token值,使用jwt生成
        String token = userService.login(user, request);
        redisTemplate.opsForValue().set(user.getPhone(), token, 72, TimeUnit.HOURS);
        return JsonResult.ok(redisTemplate.opsForValue().get(user.getPhone()));
    }

    /**
     * 用户验证码登录
     *
     * @param userCodeLoginVO
     * @return
     */
    @GetMapping("/codeLogin")
    public JsonResult<String> codeLogin(UserCodeLoginVO userCodeLoginVO, HttpServletRequest request) {
        String token = userService.codeLogin(userCodeLoginVO, request);
        redisTemplate.opsForValue().set(userCodeLoginVO.getPhone(), token, 72, TimeUnit.HOURS);
        return JsonResult.ok(redisTemplate.opsForValue().get(userCodeLoginVO.getPhone()));
    }

    /**
     * 用户手机号验证码注册
     *
     * @param registerVo
     * @return
     */
    @PostMapping("/register")
    public JsonResult<String> register(RegisterVO registerVo) {
        userService.register(registerVo);
        return JsonResult.ok("注册成功");
    }

    /**
     * 根据token获取用户信息
     *
     * @param request
     * @return
     */
    @GetMapping("/getUserInfo")
    public JsonResult<User> getUserInfo(HttpServletRequest request) {
        //调用jwt工具类方法,根据request对象获取头对象信息,返回用户id
        Integer userId = (JwtUtils.getNumberIdByJwtToken(request));
        //根据用户id,获取用户信息
        User user = userService.getUserInfoById(userId);
        return JsonResult.ok(user);
    }

    /**
     * 检验新旧密码,并更改密码
     *
     * @param updatePasswordVO
     * @return
     */
    @PostMapping("/update/password")
    public JsonResult<String> updatePassword(UpdatePasswordVO updatePasswordVO) {
        return JsonResult.ok(userService.updatePassword(updatePasswordVO));
    }

    /**
     * 找回密码时直接重置密码为:123456
     *
     * @param retrievePasswordVO
     * @return
     */
    @PostMapping("/retrieve/password")
    public JsonResult<String> retrievePassword(RetrievePasswordVO retrievePasswordVO) {
        return JsonResult.ok(userService.retrievePassword(retrievePasswordVO));
    }

    /**
     * 登出(登出时,将redis中的token删除,前端获取不到token)
     *
     * @param phone
     * @return
     */
    @GetMapping("/logoff")
    public JsonResult<String> logoff(String phone) {
        redisTemplate.delete(phone);
        return JsonResult.ok(redisTemplate.opsForValue().get(phone));
    }
}